// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include <afxcview.h>
#include "mainfrm.h"
#include "regwrap.h"
#include "capicon.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern const char cszRegKey[];

BEGIN_MESSAGE_MAP(CChildView,CWnd)
    //{{AFX_MSG_MAP(CChildView)
    ON_WM_PAINT()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
    if (!CWnd::PreCreateWindow(cs))
        return FALSE;

    cs.dwExStyle |= WS_EX_CLIENTEDGE;
    cs.style &= ~WS_BORDER;
    cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
        ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);

    return TRUE;
}

void CChildView::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
    // Do not call CWnd::OnPaint() for painting messages
}

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    //{{AFX_MSG_MAP(CMainFrame)
    ON_WM_CREATE()
    ON_WM_SETFOCUS()
    ON_WM_CLOSE()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
    ID_SEPARATOR,           // status line indicator
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
};

int CMainFrame::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
        | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
        !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
    {
        TRACE0("Failed to create toolbar\n");
        return -1;      // fail to create
    }

    if (!m_wndStatusBar.Create(this) ||
        !m_wndStatusBar.SetIndicators(indicators,
          sizeof(indicators)/sizeof(UINT)))
    {
        TRACE0("Failed to create status bar\n");
        return -1;      // fail to create
    }

    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar);

    return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    if( !CFrameWnd::PreCreateWindow(cs) )
        return FALSE;
    cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
    cs.lpszClass = AfxRegisterWndClass(0);
    
    cs.x = RegWrap( cszRegKey, true ).ReadDword( "dwWndLeft", 20 );
    cs.y = RegWrap( cszRegKey, true ).ReadDword( "dwWndTop", 20 );
    cs.cx = RegWrap( cszRegKey, true ).ReadDword( "dwWndRight", 600 ) - cs.x;
    cs.cy = RegWrap( cszRegKey, true ).ReadDword( "dwWndBottom", 400 ) - cs.y;

    return TRUE;
}

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
    CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
    CFrameWnd::Dump(dc);
}

#endif //_DEBUG

void CMainFrame::OnSetFocus(CWnd* pOldWnd)
{
    // forward focus to the view window
    m_pMainForm->SetFocus();
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
    // let the view have first crack at the command
    if (m_pMainForm->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
        return TRUE;

    // otherwise, do default handling
    return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
/////////////////////////////////////////////////////////////////////////////
// CMainForm

IMPLEMENT_DYNCREATE(CMainForm, CFormView)

CMainForm::CMainForm() : CFormView(CMainForm::IDD)
{
    font.CreatePointFont( 90, _T("Courier New"), 0 );
    //{{AFX_DATA_INIT(CMainForm)
    //}}AFX_DATA_INIT
}

void CMainForm::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CMainForm)
    DDX_Control(pDX, IDC_BTN_EXEC, m_BtnExec);
    DDX_Control(pDX, IDC_EDIT_CMD, m_EditCmdLine);
    DDX_Control(pDX, IDC_LOG, m_Log);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMainForm, CFormView)
    //{{AFX_MSG_MAP(CMainForm)
    ON_BN_CLICKED(IDC_BTN_EXEC, OnBtnExec)
    ON_WM_SIZE()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

#ifdef _DEBUG
void CMainForm::AssertValid() const
{
    CFormView::AssertValid();
}

void CMainForm::Dump(CDumpContext& dc) const
{
    CFormView::Dump(dc);
}

#endif //_DEBUG

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
    if ( !CFrameWnd::OnCreateClient(lpcs, pContext) )
        return FALSE;

    m_wndSplitter.CreateStatic( this, 1, 2 );
    m_wndSplitter.CreateView( 0, 0, RUNTIME_CLASS(CListView), CSize(90, 500), pContext );
    m_wndSplitter.CreateView( 0, 1, RUNTIME_CLASS(CMainForm), CSize(600, 500), pContext );

    m_pMainForm = (CMainForm*)m_wndSplitter.GetPane(0,1);
    m_pBCList = &((CListView*)m_wndSplitter.GetPane(0,0))->GetListCtrl();

    m_pBCList->ModifyStyleEx( 0, WS_EX_CLIENTEDGE );

    m_pBCList->InsertColumn( 0, "BC", LVCFMT_LEFT, 40 );
    m_pBCList->InsertColumn( 1, "PLCI", LVCFMT_LEFT, 50 );

    SetActiveView( (CView*)m_pMainForm );

    return TRUE;
}

void CMainForm::OnBtnExec() 
{
    CString cs;
    m_EditCmdLine.GetWindowText( cs );
    ((CCapiConApp*)AfxGetApp())->CmdLine( (LPCTSTR)cs );    
    cs = "";
    m_EditCmdLine.SetWindowText( cs );
}

void CMainForm::OnSize(UINT nType, int cx, int cy) 
{
    CFormView::OnSize(nType, cx, cy);

    if ( !m_EditCmdLine.m_hWnd )
        return;
    
    RECT rl, // Log rectangle
         rc, // Cmdline rectange
         rb; // Button rectange

    int sp = 5;        // Spacing between the controls
    int edit_h = 21;   // Edit box height
    int button_w = 70;
    int button_h = 23;

    rb.right = cx-sp;
    rb.left = rb.right-button_w;
    rb.top = cy-sp-button_h;
    rb.bottom = rb.top+button_h;
    
    rc.left = sp;
    rc.right = rb.left-sp;
    rc.top = cy-sp-1-edit_h;
    rc.bottom = rc.top+edit_h;

    rl.left = sp;
    rl.right = cx-sp;
    rl.bottom = rc.top-sp;
    rl.top = sp;

    m_EditCmdLine.MoveWindow( rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, TRUE );
    m_BtnExec.MoveWindow( rb.left, rb.top, rb.right-rb.left, rb.bottom-rb.top, TRUE );
    m_Log.MoveWindow( rl.left, rl.top, rl.right-rl.left, rl.bottom-rl.top, TRUE );
}


void CMainForm::OnInitialUpdate() 
{
    CFormView::OnInitialUpdate();
    
    m_Log.SetLimitText( (UINT)-1 );
    m_Log.SetFont( &font );
}

void CMainFrame::OnClose() 
{
    // Save the window coordinates

    CRect r;
    GetWindowRect( &r );

    RegWrap( cszRegKey, true ).WriteDword( "dwWndLeft", r.left );
    RegWrap( cszRegKey, true ).WriteDword( "dwWndTop", r.top );
    RegWrap( cszRegKey, true ).WriteDword( "dwWndRight", r.right );
    RegWrap( cszRegKey, true ).WriteDword( "dwWndBottom", r.bottom );

    CFrameWnd::OnClose();
}
